package com.handinglian.ai.template.service.impl;

import com.github.pagehelper.PageHelper;
import com.handinglian.ai.common.dto.CommonPage;
import com.handinglian.ai.common.exception.BizException;
import com.handinglian.ai.common.util.FastJsonUtil;
import com.handinglian.ai.common.util.GeneralUtil;
import com.handinglian.ai.notice.service.NoticeService;
import com.handinglian.ai.template.dto.CallTemplateDto;
import com.handinglian.ai.template.entity.CallTemplate;
import com.handinglian.ai.template.mapper.CallTemplateMapper;
import com.handinglian.ai.template.param.CallTemplateCreateParam;
import com.handinglian.ai.template.param.CallTemplateUpdateParam;
import com.handinglian.ai.template.service.CallTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service
public class CallTemplateServiceImpl implements CallTemplateService {
    @Autowired
    private CallTemplateMapper callTemplateMapper;
    @Autowired
    private NoticeService noticeService;

    @Override
    @Transactional
    public int create(CallTemplateCreateParam createParam) {
        CallTemplate callTemplate = FastJsonUtil.ObjectToObject(createParam, CallTemplate.class);
        callTemplate.setCreateTime(new Date());
        return callTemplateMapper.insertSelective(callTemplate);
    }

    @Override
    public CommonPage<CallTemplateDto> listPage(String nameOrContent, String type, String sortField, String sortOrder, Integer pageNum, Integer pageSize) {
        List<Integer> types = GeneralUtil.stringToIntegerList(type);
        PageHelper.startPage(pageNum, pageSize);
        List<CallTemplateDto> callTemplateDtos = callTemplateMapper.list(nameOrContent, types, sortField, sortOrder);
        return CommonPage.restPage(callTemplateDtos);
    }

    @Override
    @Transactional
    public int delete(Integer id) {
        Integer count = noticeService.countCallTemplate(id);
        if (count > 0){
            throw BizException.TEMPLATE_USED_ERROR;
        }
        return callTemplateMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int update(CallTemplateUpdateParam updateParam) {
        CallTemplate callTemplate = FastJsonUtil.ObjectToObject(updateParam, CallTemplate.class);
        return callTemplateMapper.updateByPrimaryKeySelective(callTemplate);
    }

    @Override
    public CallTemplateDto getDetail(Integer id) {
        CallTemplate callTemplate = callTemplateMapper.selectByPrimaryKey(id);
        return FastJsonUtil.ObjToObjWithDateFormat(callTemplate, CallTemplateDto.class);
    }

    @Override
    @Transactional
    public int setDefault(Integer id) {
        callTemplateMapper.updateIsDefault();
        return callTemplateMapper.updateIsDefaultById(id);
    }

    @Override
    @Transactional
    public int deleteBatch(String id) {
        List<Integer> ids = GeneralUtil.stringToIntegerList(id);
        return callTemplateMapper.deleteByIdIn(ids);
    }

    @Override
    public List<CallTemplateDto> list(Integer type) {
        return callTemplateMapper.findByTypeOrderByIsDefaultDesc(type);
    }
}
